package com.icss.mqsp.util;

import com.icss.mqsp.config.PropertyConfig;
import com.icss.pangu.logging.Log;
import com.icss.pangu.logging.LogFactory;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;

public class RequestCondition
{
  private static Log log = LogFactory.getLog(RequestCondition.class);
  private Map conditions = null;
  private HttpServletRequest request = null;
  
  public RequestCondition(HttpServletRequest request)
  {
    this.request = request;
    this.conditions = new HashMap();
    debug("初始化ReportCondition，request=" + request);
    debug("打印request中的值");
    printRequest();
    debug("打印condtion中的值");
    printConditon();
    debug("打印完成");
  }
  
  public RequestCondition()
  {
    this.conditions = new HashMap();
    debug("初始化ReportCondition，无request对象");
  }
  
  public static RequestCondition getRequestCondition(HttpServletRequest request)
  {
    return new RequestCondition(request);
  }
  
  public static RequestCondition getRequestCondition()
  {
    return new RequestCondition();
  }
  
  private String getRequestCondition(String key)
  {
    debug("从request中获得属性开始：key=" + key);
    String condition = null;
    if (this.request != null)
    {
      condition = this.request.getParameter(key);
      condition = encoding(condition);
      debug("request不为null，从request中获得数据condition=" + condition);
    }
    if (condition == null)
    {
      condition = "";
      debug("condition为null，置为''");
    }
    debug("最终condition=" + condition);
    return condition;
  }
  
  public Object getCondition(String key)
  {
    debug("从condition中获得参数开始");
    Object object = null;
    
    object = this.conditions.get(key);
    debug("从conditions的Map中取值object=" + object);
    if (object == null)
    {
      object = getRequestCondition(key);
      debug("conditions中不存在key值,从getRequestCondition中获取object=" + object);
    }
    debug("最终的值为：" + object);
    return object;
  }
  
  public String[] getConditions(String key)
  {
    debug("从condition中获得数组值key=" + key);
    String[] values = null;
    if (this.request != null)
    {
      values = this.request.getParameterValues(key);
      values = encoding(values);
      debug("request不为空，从request中查询：值为=" + values);
    }
    else
    {
      values = new String[0];
      debug("request为空，置空值为=" + values);
    }
    debug("最终获得值为：" + values);
    return values;
  }
  
  public void setCondition(String key, Object value)
  {
    debug("往condition中装载值key=" + key + ";value=" + value);
    this.conditions.put(key, value);
    debug("取值测试一下：key=" + key + ";value=" + this.conditions.get(key));
  }
  
  public HttpServletRequest getRequest()
  {
    return this.request;
  }
  
  public void printRequest()
  {
    debug("输出request中Parameter");
    Enumeration e = this.request.getParameterNames();
    while (e.hasMoreElements())
    {
      String key = (String)e.nextElement();
      String value = this.request.getParameter(key);
      debug(key + "=" + value);
    }
    debug("输出request中Attribute");
    Enumeration e2 = this.request.getAttributeNames();
    while (e2.hasMoreElements())
    {
      String key = (String)e2.nextElement();
      String value = this.request.getParameter(key);
      debug(key + "=" + value);
    }
  }
  
  public void printConditon()
  {
    debug("输出condition中键值对");
    Iterator iterator = this.conditions.entrySet().iterator();
    while (iterator.hasNext())
    {
      Map.Entry entry = (Map.Entry)iterator.next();
      String key = (String)entry.getKey();
      String value = (String)entry.getValue();
      debug(key + "=" + value);
    }
  }
  
  public String getConditionJsonToAjax()
  {
    StringBuffer content = new StringBuffer();
    Enumeration e = this.request.getParameterNames();
    

    content.append("{");
    while (e.hasMoreElements())
    {
      String key = (String)e.nextElement();
      String value = this.request.getParameter(key);
      content.append(key + ":" + "\"" + value + "\"").append(",");
    }
    Enumeration e2 = this.request.getAttributeNames();
    while (e2.hasMoreElements())
    {
      String key = (String)e2.nextElement();
      try
      {
        String value = (String)this.request.getAttribute(key);
        content.append(key + ":" + "\"" + value + "\"").append(",");
      }
      catch (Exception ex)
      {
        log.info(key + "非String类型");
      }
    }
    content = content.delete(content.length() - 1, content.length());
    content.append("}");
    log.info(content.toString());
    return content.toString();
  }
  
  public String getContextPath()
  {
    return this.request.getContextPath();
  }
  
  public String getRealPath()
  {
    return this.request.getRequestURI().substring(0, this.request.getRequestURI().lastIndexOf("/") + 1);
  }
  
  public String getMainCssUrl()
  {
    return getContextPath() + "/cda/css/style.css";
  }
  
  public String getCssUrl()
  {
    return getContextPath() + "/cda/css";
  }
  
  public String getIncludeUrl()
  {
    return getContextPath() + "/cda/include";
  }
  
  public String getImgUrl()
  {
    return getContextPath() + "/cda/images";
  }
  
  public String getMsgInfo()
  {
    if (this.request.getAttribute("message") != null)
    {
      String message = this.request.getAttribute("message").toString();
      if (!message.trim().equals("")) {
        return "<script>jQuery.messager.alert('系统提示', '" + message + "！', 'info');</script>";
      }
    }
    return "";
  }
  
  public String getMsgInfoEasyUi()
  {
    if (this.request.getAttribute("message") != null)
    {
      String message = this.request.getAttribute("message").toString();
      if (!message.trim().equals("")) {
        return "<script>jQuery.messager.alert('系统提示', '" + message + "！', 'info');</script>";
      }
    }
    return "";
  }
  
  public static String encoding(String from)
  {
    return from;
  }
  
  public static String[] encoding(String[] from)
  {
    String[] to = null;
    if ((from != null) && (from.length > 0))
    {
      to = new String[from.length];
      for (int i = 0; i < to.length; i++) {
        to[i] = encoding(from[i]);
      }
    }
    else
    {
      to = from;
    }
    return to;
  }
  
  private void debug(Object msg) {}
  
  public String getConditons()
  {
    String rs = "";
    if (this.conditions.size() != 0)
    {
      Iterator iterator = this.conditions.entrySet().iterator();
      while (iterator.hasNext())
      {
        Map.Entry entry = (Map.Entry)iterator.next();
        String key = (String)entry.getKey();
        String value = (String)entry.getValue();
        rs = rs + key + "=" + value + "&";
      }
      if (rs.length() > 0) {
        rs = rs.substring(0, rs.length() - 1);
      }
    }
    else
    {
      Map map = this.request.getParameterMap();
      log.info(Integer.valueOf(map.size()));
      Iterator iterator = map.keySet().iterator();
      while (iterator.hasNext())
      {
        String key = (String) iterator.next();
        String value = ((String[])map.get(key))[0];
        rs = rs + key + "=" + value + "&";
      }
      if (rs.length() > 0) {
        rs = rs.substring(0, rs.length() - 1);
      }
    }
    return rs;
  }
  
  public Map getConditionMap()
  {
    Map req = this.request.getParameterMap();
    Map reqNew = new HashMap();
    
    Iterator params = req.keySet().iterator();
    String param = null;
    String paramVal = null;
    while (params.hasNext())
    {
      param = (String)params.next();
      paramVal = ((String[])req.get(param))[0];
      reqNew.put(param, paramVal);
    }
    return reqNew;
  }
  
  public String getPamams()
  {
    Map map = this.request.getParameterMap();
    String value = "";
    

    String params = PropertyConfig.getInstance("system").getProperty("params");
    String[] list = params.split(",");
    
    int i = 0;
    
    int mapSize = map.size() - 2;
    while (mapSize > 0)
    {
      log.info(Integer.valueOf(i));
      String key = list[i].toString();
      Object mapValue = map.get(key);
      if (mapValue != null)
      {
        if (value.equals("")) {
          value = value + ((String[])map.get(key))[0];
        } else {
          value = value + "_" + ((String[])map.get(key))[0];
        }
        mapSize--;
      }
      i++;
      if (list.length - 1 == i) {
        break;
      }
    }
    return value;
  }
}
